import request from '@/utils/request.js'
import config from '@/utils/config/index.js'

/**
 * 获取字典信息
 */
export function findByTypeKey(params) {
	return request.get({
		url: '/platform/v3/cat/dictionary/findByTypeKey',
		params: params,
		loading: false
	});
}

/**
 * 获取人员信息
 */
export function query4Org(params) {
	return request.post({
		url: '/platform/v3/user/query4Org',
		params,
		type: 'platform'
	});
}

/**
 * 获取员工列表(不分页)
 */
export function queryUserByGroup(params) {
	return request.post({
		url: '/platform/v3/employee/query',
		params,
		type: 'platform'
	});
}

/**
 * 获取员工列表(分页)
 */
export function queryUserByPage(params, pager) {
	return request.post({
		url: '/platform/v3/employee/query',
		pager,
		params,
		type: 'platform'
	});
}

/**
 * 获取首页公告
 */
export function getNews() {
	return request.get({
		url: '/platform/v3/desktop/facade/getNews',
		loading: false
	});
}

/**
 * 获取公告列表
 */
export function getNoticeList(pager, params) {
	return request.post({
		url: '/platform/v3/system/news/query',
		pager,
		params,
		type: 'platform'
	});
}

/**
 * 获取公告详情
 */
export function getNoticeDetail(noticeId) {
	const params = {
		newsId: noticeId
	}
	return request.get({
		url: '/platform/v3/system/news/get',
		params
	});
}

/**
 * 获取未读消息(废弃，改用extend接口)
 */
export function getMsgList() {
	return request.get({
		url: '/platform/v3/msg/innerMessage/msgList',
		loading: false
	});
}

/**
 * 获取全部消息
 */
export function getAllMsgList(pager, params) {
	// return request.post({
	// 	url: '/platform/v3/msg/innerMessage/receive',
	// 	pager,
	// 	params,
	// 	type: 'platform'
	// });
	
	return request.post({
		url: '/extend/v3/message/queryMessageList',
		pager,
		params,
		paramType: 'post'
	});
}

/**
 * 获取消息详情
 */
export function getMsgDetail(innerMessageId) {
	return request.get({
		url: '/platform/v3/msg/innerMessage/get',
		params: { innerMessageId }
	});
}

/**
 * 获取消息已回复列表
 */
export function getMsgReply(pager, params) {
	return request.post({
		url: '/platform/v3/msg/innerMessageReply/query',
		pager,
		params,
		type: 'platform'
	});
}

/**
 * 获取消息已读列表
 */
export function getMsgRead(pager, params) {
	return request.post({
		url: '/platform/v3/msg/innerMessageRead/query',
		pager,
		params,
		type: 'platform'
	});
}

/**
 * 保存消息
 */
export function saveInnerMessage(params) {
	return request.post({
		url: '/platform/v3/msg/innerMessage/save',
		params,
		paramType: 'post'
	});
}

/**
 * 获取业务应用
 */
export function getAppResource() {
	return request.get({
		url: '/platform/v3/auth/appres/getMenuData',
		loading: false
	});
}

/**
 * 获取个人常用应用
 */
export function getFavoritesResource() {
	const params = []
	params.push({
		key: "sign",
		value: "personal"
	})
	const pager = {
		start: 1,
		limit: 50
	}
	
	return request.post({
		url: '/platform/v3/auth/appres/getMenuFavorites',
		pager,
		params,
		type: 'platform',
		paramType: 'post',
		loading: false
	});
}

/**
 * 获取用户子系统
 */
export function findCurrentUserSubSystem() {
	return request.get({
		url: '/platform/v3/auth/sub/system/findCurrentUserSubSystem',
		loading: false
	});
}

/**
 * 加载员工信息
 */
export function getUserInfo() {
	const params = {
		employeeId: uni.getStorageSync("userId")
	}
	return request.get({
		url: '/platform/v3/employee/load',
		params
	});
}

/**
 * 修改密码
 */
export function modifyPassword(params) {
	return request.post({
		url: '/platform/v3/user/change/passwd',
		params
	});
}

/**
 * 查询所有组织
 */
export function getAllOrg() {
	return request.get({
		url: '/platform/v3/org/findAll',
		loading: false
	});
}

/**
 * 获取组织数据
 */
export function findByUserId(params) {
	return request.get({
		url: '/platform/v3/org/findByUserId',
		params
	})
}

/**
 * 根据参与者类型查询等级数据
 */
export function findByType(params) {
	return request.get({
		url: '/platform/v3/level/findByType',
		params: params
	})
}

/**
 * 通过角色获取人员
 */
export function getUserByRole(params){
	let pager = {
		start: 1,
		limit: 500
	}
	return request.post({
		url: "/platform/v3/employee/queryByRoleId",
		pager,
		params,
		type: 'platform',
		paramType: 'post'
	});
}

/**
 * 获取模板信息
 */
export function queryDataByKey(params, pager) {
	return request.post({
		url: '/business/v3/data/template/queryDataByKey',
		pager,
		params,
		type: 'platform',
		paramType: 'post'
	});
}

/**
 * 获取表单数据
 */
export function getFormData(params) {
	return request.post({
		url: '/business/v3/form/def/getFormData',
		params
	});
}

/**
 * 获取用户公共信息
 */
export function getBasicInfo(userId) {
	let params = {
		userId
	}
	
	return request.post({
		url: '/extend/v3/user/userInfo',
		params,
		loading: false
	});
}

/**
 * 根据组织ID查询负责人
 */
export function queryOrgManagerByOrgId(params){
	return request.get({
		url: "/extend/v3/employee/queryOrgManagerByOrgId",
		params,
	})
}

/**
 * 查询所有公司
 */
export function getAllCompany(){
	let params = {
		parameters: [
			{ "key": "levelType", "value": "org" },// 类型为组织
			{ "key": "levelLevel", "value": "2" }// 等级为2:公司
		]
	}
	
	return request.post({
		url: "/extend/v3/entity/findAllByCondition",
		params,
		paramType: "post"
	})
}

/**
 * 查询用户绑定信息
 */
export function queryBindInfo(){
	return request.get({
		url: "/extend/v3/message/queryUserInfo",
		params: { userId: uni.getStorageSync("userId") },
		loading: false
	})
}

/**
 * 消息推送用户绑定
 */
export function bindPushUser(params){
	return request.post({
		url: "/extend/v3/message/bindUser",
		params,
		paramType: "post",
		loading: false
	})
}

/**
 * 更新用户绑定状态
 */
export function updateBindStatus(params){
	return request.post({
		url: "/extend/v3/message/updateBindStatus",
		params,
		paramType: "post",
		loading: false
	})
}

/**
 * 更新消息已读状态
 */
export function updateReadStatus(params){
	return request.post({
		url: "/extend/v3/message/updateMessageReadStatus",
		params
	})
}

/**
 * 查询未读信息数量
 */
export function queryUnreadMsgCount(){
	return request.get({
		url: "/extend/v3/message/queryUnreadMessageCount",
		params: { userId: uni.getStorageSync("userId") },
		loading: false
	})
}

/**
 * 保存业务操作日志
 */
export function saveCpisBusinessOpLog(params) {
	return request.post({
		url: '/extend/v3/CpisBusinessOpLog/save',
		params,
		paramType: 'post',
		loading: false
	});
}

/**
 * 获取app最新版本信息
 */
export async function getLatestAppVersion(terminal) {
	// return request.get({
	// 	url: '/extend/v3/cpisAppVersion/getLatestAppVersion',
	// 	params: { terminal },
	// 	loading: false
	// });
	
	let result = await uni.request({
		url: config.baseUrl + '/extend/v3/cpisAppVersion/getLatestAppVersion',
		data: { terminal },
		method: 'GET'
	})
	if(result[1]) {
		return result[1].data
	} else {
		uni.showToast({
		    title: '网络异常，请稍后重试',
		    icon: 'none'
		});
		return
	}
}

/**
 * 推送应用安装包下载成功信息
 */
export function postDownSucessInfo(params) {
	return request.post({
		url: '/extend/v3/cpisAppVersion/postDownSucessInfo',
		params,
		paramType: 'post',
		loading: false
	});
}

/**
 * 查询版本信息
 */
export function getAppVersion(params) {
	return request.post({
		url: '/extend/v3/cpisAppVersion/queryByCond',
		params,
		paramType: 'post'
	});
}

/**
 * 根据分类key获取数据字典
 */
export function findByTypeKeyExtend(typeKey) {
	return request.get({
		url: '/extend/v3/cat/myDictionary/findByTypeKey',
		params: { typeKey },
		loading: false
	});
}

/**
 * 查询序列号信息
 */
export async function getByMobileSn(mobileSn) {
	let result = await uni.request({
		url: config.baseUrl + '/extend/v3/CpisAppSn/getByMobileSn',
		data: { mobileSn },
		method: 'GET'
	})
	if(result[1]) {
		return result[1].data
	} else {
		uni.showToast({
		    title: '网络异常，请稍后重试',
		    icon: 'none'
		});
		return
	}
}

/**
 * 组织信息
 */
export function queryOrgListParameters(params) {
	return request.post({
		url: '/extend/v3/org/queryOrgListParameters',
		params,
		paramType: 'post'
	});
}

/**
 * 微服务信息
 */
export async function getLicenseMsg() {
	let result = await uni.request({
		url: config.baseUrl + '/extend/v3/license/getLicenseMsg',
		method: 'GET'
	})
	if(result[1]) {
		return result[1].data
	} else {
		uni.showToast({
		    title: '网络异常，请稍后重试',
		    icon: 'none'
		});
		return
	}
}

/**
 * 根据业务ids查询当前流程实例
 */
export function getFlowBySrcIds(ids) {
	return request.post({
		url: '/extend/v3/cpisBpmBiz/getFlowBySrcIds',
		params: ids,
		paramType: 'post'
	});
}

/**
 * 根据流程key查询当前流程实例
 */
export function getFlowByFlowKeys(flowKeys) {
	return request.post({
		url: '/extend/v3/cpisBpmBiz/getFlowByFlowKeys',
		params: flowKeys,
		paramType: 'post'
	});
}